//index.js
//获取应用实例
const app = getApp()
let imgs = [
  '../../static/image/swiper1.jpg',
  '../../static/image/swiper2.jpg', 
  '../../static/image/swiper3.jpg',
  '../../static/image/list1.jpg',
  '../../static/image/list2.jpg',
  '../../static/image/list3.jpg',
  '../../static/image/list4.jpg',
  '../../static/image/list5.jpg'
]

Page({
  data: {
    menus_chosed: 1,
    menus: [
      { title: '推荐' },
      { title: '新闻',childs:[
        { title: '首页' },
        { title: '社会' },
        { title: '国内' },
        { title: '国际' },
        { title: '民生' },
        { title: '政治' },
        { title: '历史' }
      ] },
      { title: '娱乐' ,
        childs: [
          { title: '首页' },
          { title: '热闻'},
          { title: '八卦' },
          { title: '港台' },
          { title: '影视' }
        ] },
      {
        title: '体育', childs: [
          { title: '国内' },
          { title: '国际' },
          { title: '世界杯' },
          { title: 'NBA' }
        ] },
      { title: '图片' },
      { title: '财经' },
      { title: '汽车' },
      { title: '军事' },
    ],
    menuheight:264,
    menus_child_chosed: 1,
    menus_clild: [
      { title: '首页' },
      { title: '社会' },
      { title: '国内' },
      { title: '国际' },
      { title: '民生' },
      { title: '政治' },
      { title: '历史' }
    ],
    imgUrls: [
      imgs[0],
      imgs[1],
      imgs[2]
    ],
    newList: [
      {
        title: '今日收盘：网游vr概念受挫 创业板尾盘跳水',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[2]],
        position:"left"
      },
      {
        title: '马斯克的“超级高铁”今日在美公开演示 一秒能跑300米',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[3]],
        position: "left"
      },
      {
        title: '学霸君与小米强强联手，全新miui 8系统搭载扫题功能',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[4]],
        position: "left"
      },
      {
        title: '全国人大会议日程公布:3月17日选举国家主席',
        date: '1小时',
        counts: 2345612,
        imgs: [imgs[1], imgs[0]],
        position: "bottom"
      },
      {
        title: '中国、韩国、日本智能手机普及率pk！吃惊',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[5]],
        position: "left"
      },
      {
        title: '印媒：富士康将在印度投资100亿美元，设厂生产iphone',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[6]],
        position: "left"
      },
      {
        title: '证监会严控定增 跨界虚拟产业被禁',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[7]],
        position: "left"
      },
      {
        title: '全国人大会议日程公布:3月17日选举国家主席',
        date: '1小时',
        counts: 2345612,
        imgs: [imgs[1]],
        position: "bottom"
      },
      {
        title: '今日收盘：网游vr概念受挫 创业板尾盘跳水',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[2]],
        position: "left"
      },
      {
        title: '马斯克的“超级高铁”今日在美公开演示 一秒能跑300米',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[3]],
        position: "left"
      },
      {
        title: '学霸君与小米强强联手，全新miui 8系统搭载扫题功能',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[4]],
        position: "left"
      },
      {
        title: '中国、韩国、日本智能手机普及率pk！吃惊',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[5]],
        position: "left"
      },
      {
        title: '全国人大会议日程公布:3月17日选举国家主席',
        date: '1小时',
        counts: 2345612,
        imgs: [imgs[1], imgs[0], imgs[6]],
        position: "bottom"
      },
      {
        title: '印媒：富士康将在印度投资100亿美元，设厂生产iphone',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[6]],
        position: "left"
      },
      {
        title: '证监会严控定增 跨界虚拟产业被禁',
        date: '9小时',
        counts: 5612,
        imgs: [imgs[7]],
        position: "left"
      },
    ],
    bottomLoadStatus: '',
    marginTop:164,
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    page:0,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //滚动监听
  scroll(e) {
    if(e.detail.scrollTop>164){
      this.setData({
        menuheight: 0
      })
    }else{
      this.setData({
        menuheight: 264
      })
    }
    console.log(e.detail.scrollTop);
    
  },
  onPageScroll() {
    let that = this;
    wx.createSelectorQuery().selectViewport().scrollOffset(function (res) {
      let _change = false;
      if (res.scrollTop >= 120 && that.data.title === '') {
        _change = true;
      }
      if (res.scrollTop < 120 && that.data.title !== '') {
        _change = true;
      }

    }).exec()
  },
  onPullDownRefresh() {
    let that = this;
    setTimeout(() => {
      wx.stopPullDownRefresh();
      this.setData({
        imgUrls:this.data.imgUrls.reverse()
      });
      this.setData({
        page: 0
      });
      this.setData({
        newList: this.data.newList.reverse().splice(0, 15)
      })
    }, 1200)
  },
  //上拉加载
  onReachBottom() {
    let that = this;
    let append = [];
    if (that.data.bottomLoadStatus === 'loading') return;
    that.setData({ bottomLoadStatus: "loading" });
    setTimeout(() => {
      if (that.data.page < 1) append = that.data.newList;
      if (append.length > 0) {
        that.setData({ newList: that.data.newList.concat(append) });
        that.setData({ page: that.data.page += 1 });
        that.setData({ bottomLoadStatus: "loaded" });
      } else {
        that.setData({ bottomLoadStatus: "nodata" });
      }
    }, 500)
  },
  chooseMenu(e) {
    let key = e.currentTarget.dataset['key'];
    let childs = e.currentTarget.dataset['childs'];   
    this.setData({
      menus_chosed: key
    })
    this.setData({
      menus_clild: childs || []
    })
    this.setData({
      marginTop: this.data.menus_clild.length <= 0 ? 82 : 164
    })
  },
  chooseChildMenu(e) {
    let key = e.currentTarget.dataset['key'];
    this.setData({
      menus_child_chosed: key
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
